package com.example.demo.configurer;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;

//controller通知，表示 controller 发生了异常后能够走到该通知里面执行相应的方法，是针对于 controller 的方法增强，多了异常处理
@RestControllerAdvice // @ControllerAdvice + @ResponseBody  以 json 的格式给前端返回异常信息
public class MyExceptionAdvice {
    @ExceptionHandler(Exception.class)
    public HashMap<String, Object> handler(Exception e) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("data", null);
        map.put("status", -1);
        map.put("异常msg", e.getMessage());
        return map;
    }
}
